<template>
  <div>
    <!-- 显示当前评论的cell -->
    <van-cell>
      <div class="info-box">
        <!-- 图片 -->
        <van-image fit="cover" round :src="currentItem.aut_photo" />
        <div class="txt-box">
          <div>{{ currentItem.aut_name }}</div>
          <div>{{ currentItem.content }}</div>
          <!-- 再加一行 -->
          <div class="time-box">
            <div class="time">{{ currentItem.pubdate | relvTime }}</div>
          </div>
        </div>
      </div>
    </van-cell>

    <!-- 分割线 -->
    <van-divider>回复列表</van-divider>

    <!-- 列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <div class="info-box">
          <!-- 图片 -->
          <van-image fit="cover" round :src="item.aut_photo" />
          <div class="txt-box">
            <div>{{ item.aut_name }}</div>
            <div>{{ item.content }}</div>
            <!-- 再加一行 -->
            <div class="time-box">
              <div class="time">{{ item.pubdate | relvTime }}</div>
              <van-button @click="showReply(item)" size="mini"
                >回复({{ item.reply_count }})</van-button
              >
            </div>
          </div>
          <!-- 图标 -->
          <van-icon name="like-o" />
        </div>
      </van-cell>
    </van-list>

    <!-- 发表评论区域 -->
    <div class="send-box">
      <van-field
        v-model="msg"
        center
        clearable
        placeholder="请输入要发表的评论"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>
  </div>
</template>

<script>
// 导入接口
import { commentAPI, sendCmtAPI } from "@/api/comment";
export default {
  props: {
    // 找父组件要当前被点的评论
    currentItem: {
      type: Object,
      required: true,
    },
  },

  data() {
    return {
      // 用来双向绑定输入的内容
      msg: "",
      list: [],
      loading: false,
      finished: false,
      offset: null,
    };
  },

  methods: {
    // 发表的点击事件
    async send() {
      // 非空判断
      if (this.msg == "") return this.$toast("请输入评论内容");

      // 就要发请求去发送评论
      const res = await sendCmtAPI({
        target: this.currentItem.com_id,
        content: this.msg,
        // 因为当前是发表回复，所以要传这个文章id
        art_id: this.$route.query.id
      });

      // 代表把发表成功的回复加到数组里就能立即渲染
      this.list.unshift(res.data.new_obj);
      // 给用户一个成功的提示
      this.$toast.success("发表成功！");
      // 清空输入的内容
      this.msg = "";
      // 让当前评论的回复数量+1
      this.currentItem.reply_count++
    },
    //  list加载数据写到onLoad
    async onLoad() {
      const res = await commentAPI({
        type: "c", //代表获取回复
        // 获取回复就传被点的这个评论的id
        source: this.currentItem.com_id,
        offset: this.offset,
      });

      this.offset = res.data.last_id;

      this.list.push(...res.data.results);
      // 本次查完了
      this.loading = false;

      // 如果查完了要做判断是否全部查询完，把finished改为true即可
      if (this.list.length >= res.data.total_count) {
        this.finished = true;
      }
    },
  },
};
</script>

<style>
</style>